package com.bestom.t9.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bestom.t9.R;
import com.bestom.t9mod.T9;
import com.bestom.t9mod.commons.constant.StatusCode;
import com.bestom.t9mod.commons.listener.RespSampleListener;
import com.bestom.t9mod.core.BaseActivity;
import com.bestom.t9mod.core.api.kPaApi;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class KPAActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "KPAActivity";
    private Activity mActivity;
    private Context mContext;
    private Unbinder mUnbinder;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.do_event)
    LinearLayout layout_event;
    @BindView(R.id.bt_wakeup)
    Button bt_wakeup;
    @BindView(R.id.bt_sleep)
    Button bt_sleep;
    @BindView(R.id.bt_prepare)
    Button bt_prepare;
    @BindView(R.id.bt_start)
    Button bt_start;
    @BindView(R.id.bt_stop)
    Button bt_stop;
    @BindView(R.id.bt_check)
    Button bt_check;
    @BindView(R.id.bt_version)
    Button bt_version;
    @BindView(R.id.bt_register)
    Button bt_register;
    @BindView(R.id.bt_remove)
    Button bt_remove;

    private kPaApi mKPaApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kpa);

        mContext=this;
        mActivity=this;
        mUnbinder= ButterKnife.bind(mActivity);
        mKPaApi=new kPaApi();

        T9.init();
        int flag=T9.openKPA();

        String msg = null;
        if (flag==1){
            System.out.println("打开血压计成功");
            msg="打开血压计成功";
        }else if (flag==0){
            System.out.println("打开血压计失败:没有串口读/写权限!");
            msg="打开血压计失败:没有串口读/写权限!";
            Toast.makeText(mContext,"打开血压计失败:没有串口读/写权限!",Toast.LENGTH_SHORT).show();
        }else if (flag==-1){
            System.out.println("打开血压计失败:串口未知错误!");
            msg="打开血压计失败:串口未知错误!";
        }else if (flag==-2){
            System.out.println("打开血压计失败:串口参数错误!");
            msg="打开血压计失败:串口参数错误!";
        }

        Build build=new Build();

        //region init toolbar
        //app logo
//        toolbar.setLogo(R.mipmap.ic_launcher_round);
        //title
        toolbar.setTitle("血压计测试demo");
        //sub title
//        toolbar.setSubtitle("  Subtitle");
        //以上3个属性必须在setSupportActionBar(toolbar)之前调用
        setSupportActionBar(toolbar);
        //设置导航Icon，必须在setSupportActionBar(toolbar)之后设置
        toolbar.setNavigationIcon(R.mipmap.ic_back_green);
        //添加菜单点击事件
        toolbar.setNavigationOnClickListener(v -> mActivity.finish());
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //endregion

        bt_wakeup.setOnClickListener(this);
        bt_sleep.setOnClickListener(this);
        bt_prepare.setOnClickListener(this);
        bt_start.setOnClickListener(this);
        bt_stop.setOnClickListener(this);
        bt_check.setOnClickListener(this);
        bt_version.setOnClickListener(this);
        bt_register.setOnClickListener(this);
        bt_remove.setOnClickListener(this);

        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        doBackground();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        T9.release();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        mKPaApi.Release();
//        T9.release();
    }

    private void doBackground(){
        //region 注册实时信息监听
        mKPaApi.onInfo(new RespSampleListener<Map<String, String>>() {
            @Override
            public void onSuccess(int code, Map<String, String> stringStringMap) {
                String MSB = stringStringMap.get("MSB");
                String LSB = stringStringMap.get("LSB");
                Log.i(TAG, "onInfo onSuccess: MSB" +MSB +" LSB :"+ LSB);
            }

            @Override
            public void onFailure(int code, String errMsg) {
                Log.d(TAG, "onInfo onFailure: errMsg("+errMsg+")code:"+code);
            }
        });
        //endregion

        //region 注册最后结果监听
        mKPaApi.onResult(new RespSampleListener<Map<String, String>>() {
            @Override
            public void onSuccess(int code, Map<String, String> stringStringMap) {
                String SYS_MSB = stringStringMap.get("SYS_MSB");
                String SYS_LSB = stringStringMap.get("SYS_LSB");
                String DIA_MSB = stringStringMap.get("DIA_MSB");
                String DIA_LSB = stringStringMap.get("DIA_LSB");
                String PLUSE = stringStringMap.get("PLUSE");
                Log.e(TAG, "onResult onSuccess: (SYS_MSB:" +SYS_MSB +" SYS_LSB:"+SYS_LSB+" DIA_MSB"+DIA_MSB+" DIA_LSB"+DIA_LSB+" PLUSE"+PLUSE+")");
                Toast.makeText(getApplicationContext(),"onResult onSuccess: (SYS_MSB:" +SYS_MSB +" SYS_LSB:"+SYS_LSB+" DIA_MSB"+DIA_MSB+" DIA_LSB"+DIA_LSB+" PLUSE"+PLUSE+")",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int code, String errMsg) {
                Log.d(TAG, "onResult onFailure: errMsg("+errMsg+")code:"+code);

                Toast.makeText(getApplicationContext(),"onResult onFailure: errMsg("+errMsg+")code:"+code,Toast.LENGTH_SHORT).show();
            }
        });
        //endregion

        //region 注册报错监听
        mKPaApi.onError(new RespSampleListener<Map<String, String>>() {
            @Override
            public void onSuccess(int code, Map<String, String> stringStringMap) {
                String ERROR_CODE = stringStringMap.get("ERROR_CODE");
                String ERROR_MSG = stringStringMap.get("ERROR_MSG");

                Log.d(TAG, "onError onSuccess: ERROR_MSG("+ERROR_MSG+")ERROR_CODE"+ERROR_CODE);
                Toast.makeText(getApplicationContext(),"onError onSuccess: ERROR_MSG("+ERROR_MSG+")ERROR_CODE"+ERROR_CODE,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int code, String errMsg) {
                Log.d(TAG, "onError onFailure: errMsg("+errMsg+")code:"+code);
                Toast.makeText(getApplicationContext(),"onError onFailure: errMsg("+errMsg+")code:"+code,Toast.LENGTH_SHORT).show();
            }
        });
        //endregion
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_wakeup:
                mKPaApi.wakeup(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        runOnUiThread(() -> {
                            if (layout_event != null && bt_wakeup != null){
                                layout_event.setVisibility(View.VISIBLE);
                                bt_wakeup.setBackgroundResource(R.color.lightgreen);
                            }
                        });
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        Log.d(TAG, "onResult onFailure: errMsg("+errMsg+")code:"+code);
                        runOnUiThread(()->{
                            if (layout_event != null && bt_wakeup != null){
                                layout_event.setVisibility(View.GONE);
                                bt_wakeup.setBackgroundResource(R.color.red);
                            }
                        });
                    }
                });
                break;
            case R.id.bt_sleep:
                layout_event.setVisibility(View.GONE);
                mKPaApi.sleep(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        runOnUiThread(()->{
                            if (layout_event != null && bt_sleep != null){
                                layout_event.setVisibility(View.GONE);
                                bt_sleep.setBackgroundResource(R.color.lightgreen);
                            }

                        });
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        if (bt_sleep != null){
                            runOnUiThread(()->bt_sleep.setBackgroundResource(R.color.red));
                        }

                    }
                });

                break;
            case R.id.bt_prepare:
                mKPaApi.prepare(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        runOnUiThread(()-> bt_prepare.setBackgroundResource(R.color.lightgreen));
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        runOnUiThread(()->bt_prepare.setBackgroundResource(R.color.red));
                    }
                });
                break;
            case R.id.bt_start:
                mKPaApi.start(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        runOnUiThread(()->bt_start.setBackgroundResource(R.color.lightgreen));
                        Log.e(TAG, "********onSuccess: start*******" );
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        runOnUiThread(()->bt_start.setBackgroundResource(R.color.red));
                    }
                });
                break;
            case R.id.bt_stop:
                mKPaApi.stop(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        runOnUiThread(()-> bt_stop.setBackgroundResource(R.color.lightgreen));
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        runOnUiThread(()->bt_stop.setBackgroundResource(R.color.red));
                    }
                });
                break;
            case R.id.bt_check:
                mKPaApi.check(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        runOnUiThread(()->
                                bt_check.setBackgroundResource(R.color.lightgreen));
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        runOnUiThread(()->
                                bt_check.setBackgroundResource(R.color.red));
                    }
                });
                break;
            case R.id.bt_version:
                mKPaApi.version(new RespSampleListener() {
                    @Override
                    public void onSuccess(int code, Object o) {
                        if (code== StatusCode.SUCCESS.getCode()){
                            runOnUiThread(() -> {
                                Toast.makeText(mContext,(String)o,Toast.LENGTH_SHORT).show();
                                bt_version.setBackgroundResource(R.color.lightgreen);
                            });
                        }
                    }

                    @Override
                    public void onFailure(int code, String errMsg) {
                        runOnUiThread(() -> bt_version.setBackgroundResource(R.color.red));
                    }
                });
                break;
            case R.id.bt_register:
                doBackground();
                break;
            case R.id.bt_remove:
//                mKPaApi.RemoveOnListener(KPARECmdCode.KPA_RECMD_INFO);
//                mKPaApi.RemoveOnListener(KPARECmdCode.KPA_RECMD_RESULT);
//                mKPaApi.RemoveOnListener(KPARECmdCode.KPA_RECMD_ERROR);

                mKPaApi.Release();

                break;
            default:
                break;
        }
    }
}
